package es.uji.viselab.model;

import es.uji.viselab.image.camera.CameraCatalog;
import es.uji.viselab.math.Matrix4;
import es.uji.viselab.math.Vector2;
import es.uji.viselab.math.Vector3;

public class ModelCreator {

//	private ModelCatalog mc;
//	private CameraCatalog cc;
//	private Matrix4 target;
/*
	
	public ModelCreator(ModelCatalog mc, CameraCatalog cc, Matrix4 target) {
		this.mc = mc;
		this.cc = cc;
		this.target = target;
	}

	public ModelCreator(ModelCatalog mc, CameraCatalog cc) {
		this.mc = mc;
		this.cc = cc;
		Matrix4 m = Matrix4.getIdentityM();
		this.target = m;
	}

	public ModelCreator(ModelCatalog mc, CameraCatalog cc,double x, double y, double z) {
		this.mc = mc;
		this.cc = cc;
		// This is the position of the target
		Matrix4 t = Matrix4.getTranslation(x, y, z);
		// I am not worried about the orientation, 
		// as far the z axis is going down
		Matrix4 r = Matrix4.getIdentityM();
		r.rotY(Math.PI);
		t.mul(r);
		this.target = t;
	}

	public ModelCreator(ExperimentParameters ep) {
		this.mc = ep.getModel();
		this.cc = ep.getCamParameters();
		this.target = ep.getTarget();
	}
	*/
	public static Vector2[] get2d(ModelCatalog mc, CameraCatalog cc){
		Vector2[] points2d = new Vector2[4];
		Vector3[] square = mc.getModel();
		//double height = cc.getImageHeight() / 2; // center of the image (in pixels)
		//double width = cc.getImageWidth() / 2;
		// The square will be 200px x 200px
		for (int i = 0; i < 4; i++) {
			points2d[i] = new Vector2(square[i].x * 100 ,  square[i].y * 100);
		}
		return points2d;
	}
	
	

	public static Vector3[] get3d(ModelCatalog mc, CameraCatalog cc, Matrix4 target){
		Vector3[] points2d = mc.getModel();
		Vector3[] points3d = new Vector3[points2d.length];
		// Matrix4 transformation = target.getInverseHomogenousTransformation();
		for (int i = 0; i < points2d.length; i++) {
			// The real 3d object is a square of 6 cm. 
			// (3+3) separated points each (3 cm = 0.03 m)
			points2d[i].x = points2d[i].x * 0.03;
			points2d[i].y = points2d[i].y * 0.03;
			// The square will be 100 px. x 100 px on the camera frame
			// and 0.03 mm x 0.03 mm on the world fram
			points2d[i].z = cc.getFocalDistanceCorrected()*0.03/100;
			points3d[i] = target.mul(points2d[i]);
		}
		return points3d;
	}
	
	
}
